package com.app.service.impl;

import com.app.mapper.CategoryPhoneMapper;
import com.app.model.CategoryPhoneModel;
import com.app.service.CategoryPhoneService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class CategoryPhoneServiceImpl implements CategoryPhoneService {
    @Autowired
    CategoryPhoneMapper categoryPhoneMapper;

    @Override
    public void insert(CategoryPhoneModel model) {
        categoryPhoneMapper.insert(model);
    }

    @Override
    public void update(CategoryPhoneModel model) {
        categoryPhoneMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        categoryPhoneMapper.deleteById(id);
    }


    @Override
    public CategoryPhoneModel getCategoryPhoneById(int id) {
        return categoryPhoneMapper.selectById(id);
    }

    @Override
    public List<CategoryPhoneModel> getPhoneListByPid(int pid) {
        QueryWrapper<CategoryPhoneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("pid", pid);
//
        List<CategoryPhoneModel> list = categoryPhoneMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public CategoryPhoneModel getCatPhoneByPidAndId(int pid, int id) {
        QueryWrapper<CategoryPhoneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name").eq("pid", pid).eq("id", id);
        CategoryPhoneModel model = categoryPhoneMapper.selectOne(queryWrapper);
        return model;
    }

    @Override
    public List<CategoryPhoneModel> selectPhoneTree(String keyWord) {
        QueryWrapper<CategoryPhoneModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (StringTool.isNotEmpty(keyWord)) {
            queryWrapper.like("name", keyWord);
        }
        List<CategoryPhoneModel> modelList = categoryPhoneMapper.selectList(queryWrapper);
        return modelList;
    }
}
